import * as THREE from 'three';
import Camera from './camControl';
import Floor from './floor';
import Wall from './wall';
import Storage from './storage';
/**
 * 场景类
 * 主要用于设置场景、渲染器、相机、灯光、地板、门窗墙、仓库货架
 */
export default class Model{
    //////////////////////////////////////////// SCENE SETUP 设置场景 /////////////////////////////////////////////
    // 01: Construct the scene 构建场景
    constructor(_main) {
        console.log('场景类')
        // Accelerometer orientation vars
        this.pageMain = _main;
        this.vpW = this.pageMain.clientWidth;
        this.vpH = this.pageMain.clientHeight;
        // 初始化场景
        this.scene = new THREE.Scene();
        this.renderer = new THREE.WebGLRenderer({ antialias: true });
        this.renderer.setPixelRatio(1);
        this.renderer.setSize(this.vpW, this.vpH);
        this.renderer.setClearColor(0x393533); // 设置场景颜色及其透明度
        this.renderer.autoClear = false; // 渲染每一帧之前不会自动清除其输出
        // Set up cameras // 照相机参数
        this.camOptions = { distance:50 };
        this.monoc = new Camera(this.camOptions); // 新建照相机
        this.cam = this.monoc;
        this.pageMain.appendChild(this.renderer.domElement); // 将3js场景加入到dom对象中
        this.addSceneLights(); // 设置场景灯光
    }
    // 02: 预加载开始
    introPreloading = function () {
    };
    // 03: 预加载结束
    introPreloaded = function (_cargo) {
        
        // 加载天空
        this.skyRadiance = _cargo["envSkybox"];
        this.skyRadiance.format = THREE.RGBFormat;
        // 将天空设置为背景
        this.scene.background = this.skyRadiance;

        // 加载仓库
        this.storage = new Storage({ _scene :this.scene, _cargo });
        // 加载地板
        this.Floor = new Floor(this.scene,_cargo);
        // 加载墙
        this.Wall = new Wall(this.scene,_cargo);
        
    };
    // 04:  引导页切换动画
    introStart = function () {
        // TweenLite.to(this.cam, 1.0, { distTarget:20, distActual: 10 });
        TweenLite.to(this.spotLight, 3.0, { intensity: 1.0 });
    };
    // 将灯光添加到场景
    addSceneLights = function () {
        this.ambLight = new THREE.AmbientLight(0xffffff, 0.3); // 氛围光
        this.spotLight = new THREE.DirectionalLight(0xffffff, 0.0); // 平行光
        this.spotLight.position.set(0, 40, 0); // 设置平行光位置
        this.scene.add(this.spotLight);
        this.scene.add(this.ambLight);
    };
    ////////////////////////////////// 控制事件 //////////////////////////////////
    onWindowResize = function () {
        this.vpW = this.pageMain.clientWidth;
        this.vpH = this.pageMain.clientHeight;
        this.renderer.setSize(this.vpW, this.vpH);
        this.monoc.onWindowResize(this.vpW, this.vpH);
    };
    //////////////////////////////////////////// 更新	/////////////////////////////////////////////
    updateIntro = function () {
        this.renderer.clear();
        this.cam.update();
        this.renderer.render(this.scene, this.monoc.camera);
    };
    update = function (time, _stereo) {
        this.renderer.clear();
        this.cam.update();
        this.spotLight.position.copy(this.monoc.camera.position).normalize();
        this.spotLight.position.y = 1;
        this.renderer.render(this.scene, this.monoc.camera, null, false);
    };
}